package com.madou.dbtool.redis.panel;

import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.event.DBEvent;
import com.madou.dbtool.event.DBEventManager;
import com.madou.dbtool.event.EventPanel;
import com.madou.dbtool.language.enums.ContentTypeEnum;
import com.madou.dbtool.redis.constants.RedisIds;
import com.madou.dbtool.redis.event.OpenRedisClientEvent;
import com.madou.dbtool.redis.event.OpenRedisDataEvent;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.redis.terminal.RedisTerminal;
import com.madou.dbtool.ui.content.TitleNo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

/**
 * redis的客户端面板
 * @author miukoo
 */
@HideClass
public class RedisClientEventPanel extends RedisTerminal implements EventPanel, TitleNo {
    String title;
    // 节点新
    RedisSourceManager redisSourceManager;
    int dbIndex = 0;
    JButton button;

    public RedisClientEventPanel() {
        super(ContentTypeEnum.REDIS);
        this.setLayout(null);
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                resizedButton();
            }
        });
        initButton();
    }

    private void initButton(){
        button = new JButton("浏览数据",AllIcons.MENU.TABLE_DATA);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DBEventManager.notice(new OpenRedisDataEvent(RedisIds.EVENT_ID_DATAVIEW+""+dbIndex,redisSourceManager,null,null,dbIndex));
            }
        });
        this.add(button);
    }

    public static DBEvent bingEvent() {
        return OpenRedisClientEvent.NAME;
    }

    @Override
    public void actionEvent(DBEvent event) {
        if(event instanceof OpenRedisClientEvent){
            OpenRedisClientEvent e = (OpenRedisClientEvent)event;
            redisSourceManager = e.getRedisSourceManager().cloneCluster();
            this.setRedisSourceManager(redisSourceManager);
            this.dbIndex = e.getDbIndex();
            this.title = String.format("[%d]Client-%s",e.getDbIndex(),e.getRedisSourceManager().getDataSourceName());
            this.setTitle(title);
            this.title = this.getTitle(this.title);//自动序号
            redisSourceManager.execute(e.getDbIndex(),"select "+e.getDbIndex());
        }
    }

    /**
     * 重新定位按钮位置
     */
    private void resizedButton(){
        int width = getWidth();
        button.setBounds(width-130,10,120,30);
    }

    @Override
    public String title() {
        return title;
    }

    /**
     * 是否可以滚动
     * @return
     */
    @Override
    public boolean canScroll(){
        return true;
    }

    @Override
    public Icon icon(){
        return AllIcons.Common.TERMINAL;
    }

    /**
     * 关闭按钮的回调函数
     * @return
     */
    @Override
    public void onClose(){
        this.clearTitle(title);
        getRedisSourceManager().close();
    }

    @Override
    public int getDBIndex() {
        return dbIndex;
    }
}
